package com.dora.algorithm.huawei;

import java.util.*;

/**
 * 磁盘容量排序
 *
 * @author hanchen
 * @date 2024/02/20
 */
public class SortDiskCapacity {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());

        //接收数据
        ArrayList<String> list = new ArrayList<>();
        for(int i = 0; i < n; i++){
            list.add(sc.nextLine());
        }

        list.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return getChange(o1) - getChange(o2);
            }
        });

        for(String s : list){
            System.out.println(s);
        }
    }


    //辅助函数
    private static int getChange(String s){
        String temp = s.toUpperCase();
        String[] split = temp.split("[A-Z]");
        //记录s同一单位后的数值
        int sum = 0;
        //定位单位位置所用
        int length = 0;

        for(int i = 0; i < split.length; i++){
            int num = Integer.parseInt(split[i]);
            length += split[i].length();
            char c = temp.charAt(length);
            switch(c){
                case 'M' :
                    sum += num;
                    break;
                case 'G' :
                    sum += num * 1024;
                    break;
                case 'T' :
                    sum += num * 1024 * 1024;
                    break;
                default:
                    throw new RuntimeException();
            }
            length++;
        }
        return sum;
    }
}
